package nuist.hanniko.stu.webbackend.dao.mapper;

import nuist.hanniko.stu.webbackend.model.LwM2m.LwM2MResourceValue;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public interface HistoryRecordMapper {

    @Select("select * from device_record " +
            "where device_id = #{device_id} and object_id = #{object_id} and resource_id = #{resource_id}")
    List<LwM2MResourceValue> getResource(@Param("device_id") String deviceId,
                                   @Param("object_id") String objectId,
                                   @Param("resource_id") String resourceId);

    @Select("select * from device_record " +
            "where device_id = #{device_id} and object_id = #{object_id} and resource_id = #{resource_id}" +
            "and timestamp > #{begin} and timestamp < #{end}")
    List<LwM2MResourceValue> getResourceWithinPeriod(@Param("device_id") String deviceId,
                                               @Param("object_id") String objectId,
                                               @Param("resource_id") String resourceId,
                                               @Param("begin") Date begin,
                                               @Param("end") Date end);

    @Select("select * from device_record " +
            "where device_id = #{device_id} and object_id = #{object_id}")
    List<LwM2MResourceValue> getByDeviceIdAndObjectId(@Param("device_id") String deviceId,
                                                      @Param("object_id") String objectId);

    @Select("select * from device_record " +
            "where device_id = #{device_id} and object_id = #{object_id}" +
            "and timestamp > #{begin} and timestamp < #{end}")
    List<LwM2MResourceValue> getByDeviceIdAndObjectIdWithinPeriod(@Param("device_id") String deviceId,
                                                                  @Param("object_id") String objectId,
                                                                  @Param("begin") Date begin,
                                                                  @Param("end") Date end);

    @Select("select * from device_record " +
            "where device_id = #{device_id}")
    List<LwM2MResourceValue> getByDeviceId(@Param("device_id") String deviceId);


    @Select("select * from device_record " +
            "where device_id = #{device_id} and timestamp > #{begin} and timestamp < #{end}")
    List<LwM2MResourceValue> getByDeviceIdWithinPeriod(@Param("device_id") String deviceId,
                                                       @Param("begin") Date begin,
                                                       @Param("end") Date end);

    @Insert("insert ignore into device_record(device_id, object_id, resource_id, value_type, value, timestamp)" +
            "value (#{deviceId}, #{objectId}, #{resourceId}, #{valueType}, #{value}, #{timestamp})")
    void insert(LwM2MResourceValue resourceValue);

    @Insert({
            "<script>",
            "insert ignore into device_record(device_id, object_id, resource_id, value_type, value, timestamp) ",
            "values ",
            "<foreach collection='resourceValues' item='item' separator=','>",
            "( #{item.deviceId}, #{item.objectId}, #{item.resourceId}, #{item.valueType}, #{item.value}, #{item.timestamp})",
            "</foreach>",
            "</script>"
    })
    void insertBatch(@Param("resourceValues") List<LwM2MResourceValue> resourceValues);
}
